package com.bocloud.cmp.dao.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bocloud.cmp.dao.ApproveNodeDao;
import com.bocloud.cmp.entity.ApproveNode;
import com.bocloud.common.model.Param;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.JdbcGenericDao;
import com.bocloud.database.utils.SQLHelper;

/**
 * 审批DAO接口实现
 * 
 * @author dongkai
 *
 */
@Repository("approveNodeDao")
public class ApproveNodeDaoImpl extends JdbcGenericDao<ApproveNode, Long> implements ApproveNodeDao {

	@Override
	public List<ApproveNode> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select * from approve_node a where a.is_deleted = 0";
		sql = SQLHelper.buildRawSql(sql, page, rows, params, sorter, "a");
		return this.list(ApproveNode.class, sql);
	}

	@Override
	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from approve_node a where a.is_deleted = 0";
		sql = SQLHelper.buildRawSql(sql, params, null, "a");
		Map<String, Object> param = SQLHelper.getParam(params);
		return this.countQuery(sql, param).intValue();
	}

	@Override
	public boolean delete(Long id, Long userId) throws Exception {
		String sql = "update approve_node set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	@Override
	public ApproveNode query(Long id) throws Exception {
		String sql = "select * from approve_node a where a.id = :id and a.is_deleted = 0";
		Map<String, Object> paramMap = MapTools.simpleMap("id", id);
		List<ApproveNode> list = this.list(ApproveNode.class, sql, paramMap);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public List<ApproveNode> listByAid(Long applyId) throws Exception {
		String sql = "select a.*,b.name username from approve_node a left join user b on a.approver = b.id where a.is_deleted = 0 and a.apply_id = :applyId order by a.priority";
		Map<String, Object> param = MapTools.simpleMap("applyId", applyId);
		return this.list(ApproveNode.class, sql, param);
	}

	@Override
	public List<ApproveNode> listByUid(Long userId, String status) throws Exception {
		String sql = "select * from approve_node a where a.is_deleted = 0 and a.status = :status and a.approver = :userId order by a.priority";
		Map<String, Object> params = MapTools.simpleMap("userId", userId);
		params.put("status", status);
		return this.list(ApproveNode.class, sql, params);
	}

	@Override
	public ApproveNode get(Long applyId, Integer priority) throws Exception {
		String sql = "select * from approve_node a where a.is_deleted = 0 and a.apply_id = :applyId and a.priority = :priority";
		Map<String, Object> params = MapTools.simpleMap("applyId", applyId);
		params.put("priority", priority);
		List<ApproveNode> approveNodes = this.list(ApproveNode.class, sql, params);
		if (approveNodes.isEmpty()) {
			return null;
		}
		return approveNodes.get(0);

	}

}
